package org.quasar.airNova.presentationLayer;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.quasar.airNova.businessLayer.CalendarDate;

public class CancelarVoo extends JPanel {
	private static final long serialVersionUID = 1L;
	private Gui gui;
	private JComboBox<String> data;
	private JLabel lblDataDeOcorrncia;
	private JComboBox<Object> comboBox;

	/**
	 * Create the panel.
	 */
	public CancelarVoo(Gui gui) {
		setGui(gui);
		setLayout(new BorderLayout(0, 0));
		this.setSize(500, 350);
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Voo");
		lblNewLabel.setBounds(40, 70, 151, 14);
		panel.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("Apagar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedItem() != null && data.getSelectedItem() != null)
				AirNova_Database.cancelarVoo((String) comboBox.getSelectedItem(),(String) data.getSelectedItem() );
			}
		});
		btnNewButton.setBounds(232, 248, 98, 23);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Fechar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getTabbedPane().removeTabAt(getGui().getTabbedPane().getSelectedIndex());
			}
		});
		btnNewButton_1.setBounds(340, 248, 89, 23);
		panel.add(btnNewButton_1);
		
		JLabel lblNewLabel_3 = new JLabel("Cancelar Voo");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblNewLabel_3.setBounds(30, 24, 161, 32);
		panel.add(lblNewLabel_3);
		
		if(Tools.getVoosNumero() != null)
		comboBox = new JComboBox(Tools.getVoosNumero());
		else
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedItem() != "") {
					CalendarDate[] dates = Tools.getVoosData((String)comboBox.getSelectedItem());
					data.removeAllItems();
					String[] stringDates = arrangeToString(dates);
					for (int i = 0; i < stringDates.length; i++) {
						data.addItem(stringDates[i]);
					}
				}	
			}

			private String[] arrangeToString(CalendarDate[] dates) {
				String[] vector = new String[dates.length];
				int counter = 0;
				for (CalendarDate each : dates) {
					if(each != null){
						String temp = each.day()+":"+each.month()+":"+each.year();
						vector[counter] = temp;
						counter++;
					}
				}
				return vector;
			}
		});
		comboBox.setBounds(226, 67, 203, 20);
		panel.add(comboBox);
		
		lblDataDeOcorrncia = new JLabel("Data");
		lblDataDeOcorrncia.setBounds(40, 106, 98, 14);
		panel.add(lblDataDeOcorrncia);
		
		data = new JComboBox<String>();
		data.setBounds(226, 103, 203, 20);
		panel.add(data);

	}

	public Gui getGui() {
		return gui;
	}

	public void setGui(Gui gui) {
		this.gui = gui;
	}

}